import { Language } from "../../../../../extend/language/Language";
import { EquipmentModuleConst } from "../../equipment/data/EquipmentModuleConst";
import { ObjCreate } from "../../common/ObjCreate";
import { GTabBar } from "../../../../../com/mui/controls/GTabBar";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { MuiEvent } from "../../../../../com/mui/events/MuiEvent";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { TilePanel } from "../../equipment/TilePanel";
import { MaterialJewelCellRenderer } from "../../equipment/MaterialJewelCellRenderer";
import { ListEvent } from "../../../../../fl/events/ListEvent";
import { JewelExchangeNormal } from "./JewelExchangeNormal";
import { JewelExchangeSpecial } from "./JewelExchangeSpecial";
import { MsgManager } from "../../../manager/MsgManager";
import { ItemData } from "../../../resource/info/ItemData";
import { Cache } from "../../../cache/Cache";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { BuyAlert } from "../../pet/BuyAlert";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { TodayNoTipsConst } from "../../../../component/gconst/TodayNoTipsConst";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../../common/alertwins/CheckBoxWin";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ScaleBitmap } from "../../../../../com/mui/display/ScaleBitmap";
import { IJewelExchangeOper } from "./IJewelExchangeOper";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GLabel } from "../../../../../com/mui/controls/GLabel";
type int = number;
//class JewelExchangePanel
    
    export  class JewelExchangePanel /* flash.display.Sprite */
    {
        constructor()
        {
            this._tabbarData = [{"name":"", "label": Language.getString(69226), "type":""}, {"name":"", "label": Language.getString(69227), "type":""}];
            this._desc = Language.getString(69214);
            
            this.createChildren();
            this.addEventListener(EquipmentModuleConst.Event_JewelExchangeUpdateJewels, this.onUpdateJewelData);
            return;
        }

        protected createChildren(): void
        {
            ObjCreate.createBg(21, 60, 196, 206, this);
            ObjCreate.createBg(21, 272, 196, 184, this);
            this._operBg = ObjCreate.createBg(222, 60, 387, 365, this);
            this._typeTabBar = new GTabBar();
            this._typeTabBar.buttonWidth = 62;
            this._typeTabBar.x = 30;
            this._typeTabBar.y = 67;
            this._typeTabBar.horizontalGap = 1;
            this._typeTabBar.buttonFilters = [FilterConst.glowFilter];
            this._typeTabBar.dataProvider = this._tabbarData;
            this._typeTabBar.addEventListener(MuiEvent.GTABBAR_SELECTED_CHANGE, this.opertionChangeHandler);
            this.addChild(this._typeTabBar);
            ObjCreate.createBg(31, 88, 176, 2, this, ImagesConst.SplitLine);
            ObjCreate.createLabel(Language.getString(69216), 31, 277, 100, 20, flash.text.TextFormatAlign.LEFT, this, false, GlobalStyle.color1Uint);
            ObjCreate.createBg(31, 297, 176, 2, this, ImagesConst.SplitLine);
            this._descText = ObjCreate.createTextField("", 31, 302, 176, 150, this);
            this._descText.filters = [];
            this._descText.wordWrap = true;
            this._descText.htmlText = "<textformat leading=\'3\' leftMargin=\'18\' indent=\'-18\'>" + this._desc + "</textformat>";
            this._jewelPanel = new TilePanel();
            this._jewelPanel.x = 31;
            this._jewelPanel.y = 95;
            this._jewelPanel.setSize(182, 161);
            this._jewelPanel.rowHeight = 40;
            this._jewelPanel.columnWidth = 40;
            this._jewelPanel.setStyle("cellRenderer", MaterialJewelCellRenderer);
            this.addChild(this._jewelPanel);
            this._jewelPanel.addEventListener(ListEvent.ITEM_CLICK, this.onJewelItemClick);
            ObjCreate.createLabel(Language.getString(69219), 226, 435, 70, 20, flash.text.TextFormatAlign.LEFT, this, false, 16776960);
            ObjCreate.createBg(290, 433, 80, 22, this);
            this._unitBm = ObjCreate.createBitmap(ImagesConst.Tongqian, 349, 435, this);
            this._costLabel = ObjCreate.createLabel("", 291, 433, 80, 22, flash.text.TextFormatAlign.LEFT, this, false, GlobalStyle.color1Uint);
            this._startBtn = ObjCreate.createGButton(Language.getString(69220), 517, 430, 80, 26, this, "ButtonSpecial");
            this._startBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onStartTransClick);
            this.updateOperateArea();
            this.updateJewelDatas();
            return;
        }

        /* internal  */opertionChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this.updateOperateArea();
            this.updateJewelDatas();
            return;
        }

        /* internal  */updateOperateArea(): void
        {
            var loc1=this._typeTabBar.selectedItem["label"];
            switch (loc1) 
            {
                case Language.getString(69226):
                {
                    if (!this._normalJewelOper) 
                    {
                        this._normalJewelOper = new JewelExchangeNormal();
                        this._normalJewelOper.x = this._operBg.x;
                        this._normalJewelOper.y = this._operBg.y;
                    }
                    if (this._specialJewelOper && this._specialJewelOper.parent) 
                    {
                        this._specialJewelOper.parent.removeChild(this._specialJewelOper);
                    }
                    this.addChild(this._normalJewelOper);
                    this._currentOper = this._normalJewelOper;
                    break;
                }
                case Language.getString(69227):
                {
                    if (!this._specialJewelOper) 
                    {
                        this._specialJewelOper = new JewelExchangeSpecial();
                        this._specialJewelOper.x = this._operBg.x;
                        this._specialJewelOper.y = this._operBg.y;
                    }
                    if (this._normalJewelOper && this._normalJewelOper.parent) 
                    {
                        this._normalJewelOper.parent.removeChild(this._normalJewelOper);
                    }
                    this.addChild(this._specialJewelOper);
                    this._currentOper = this._specialJewelOper;
                    break;
                }
            }
            return;
        }

        /* internal  */onJewelItemClick(arg1: ListEvent): void
        {
            var loc1=null;
            var loc2=false;
            var loc3=null;
            if (this._currentOper.hasFullPutIn() && !arg1.item["selected"]) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69221));
            }
            else 
            {
                loc1 = arg1.item["data"] as ItemData;
                loc2 = arg1.item["selected"];
                if (loc2) 
                {
                    this._currentOper.removeJewel(loc1);
                }
                else 
                {
                    this._currentOper.addJewel(loc1);
                }
                this.updateJewelDatas(this._jewelPanel.dataProvider);
            }
            return;
        }

        /* internal  */onStartTransClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc4=null;
            var loc5=null;
            if (!this._currentOper.hasJewelPutIn()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69222));
                return;
            }
            var loc1=false;
            var loc2=this._currentOper.getCost();
            var loc3=Cache.instance.role.money.coinBind + Cache.instance.role.money.coin;
            if (this.unit != EPrictUnit._EPriceUnitGold) 
            {
                loc1 = loc2 <= loc3;
            }
            else 
            {
                loc1 = BuyAlert.checkMoney(this.unit, loc2);
            }
            if (loc1) 
            {
                if (!PulseSharedObject.isTodayNotTips(TodayNoTipsConst.JewelExchangeTodayNoTips) && this._currentOper is JewelExchangeSpecial) 
                {
                    Alert.alertWinRenderer = CheckBoxWin;
                    loc4 = HTMLUtil.addColor(this._currentOper.getCost() + GameDefConfig.instance.getEPrictUnitName(this.unit), GlobalStyle.color6);
                    loc5 = Language.getStringByParam(69228, loc4);
                    Alert.show(loc5, null, Alert.OK | Alert.CANCEL, null, this.onAlertClose);
                }
                else 
                {
                    this.onStarTransImpl();
                }
            }
            else if (this.unit == EPrictUnit._EPriceUnitCoin) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, loc2 - loc3));
            }
            return;
        }

        /* internal  */onAlertClose(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                this.onStarTransImpl();
            }
            if (arg2) 
            {
                PulseSharedObject.save(TodayNoTipsConst.JewelExchangeTodayNoTips, true);
            }
            return;
        }

        /* internal  */onStarTransImpl(): void
        {
            var loc1=this._currentOper.getTransJewelDic();
            var loc2=this._currentOper.getTargetJewels();
            Dispatcher.dispatchEvent(new DataEvent(EventName.SmeltJewelTransStarTrans, {"jewelDic": loc1, "targetJewelsDic": loc2}));
            return;
        }

        /* internal  */onUpdateJewelData(arg1: DataEvent): void
        {
            this.updateJewelDatas(this._jewelPanel.dataProvider);
            return;
        }

        public updateJewelDatas(arg1: DataProvider=null): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=0;
            this.checkJewelLost();
            if (arg1) 
            {
                loc3 = 0;
                while (loc3 < arg1.length) 
                {
                    loc2 = arg1.getItemAt(loc3);
                    loc2["selected"] = Cache.instance.smelt.isSelected((loc2["data"] as ItemData).uid);
                    ++loc3;
                }
            }
            else 
            {
                loc1 = this._currentOper is JewelExchangeNormal ? 0 : 1;
                arg1 = new DataProvider(Cache.instance.smelt.getAllJewels(loc1));
            }
            this._jewelPanel.dataProvider = arg1;
            return;
        }

        public checkJewelLost(): void
        {
            if (this._currentOper) 
            {
                this._currentOper.checkJewelLost();
                this.updateJewelTransCost();
            }
            return;
        }

        public updateJewelTransCost(): void
        {
            var loc4=0;
            var loc5=0;
            var loc1=this._currentOper.getCost();
            var loc2=true;
            if (this.unit != EPrictUnit._EPriceUnitGold) 
            {
                if (this.unit == EPrictUnit._EPriceUnitCoin) 
                {
                    loc5 = Cache.instance.role.money.coinBind + Cache.instance.role.money.coin;
                    if (loc1 > loc5) 
                    {
                        loc2 = false;
                    }
                }
            }
            else 
            {
                loc4 = Cache.instance.role.money.gold;
                if (loc1 > loc4) 
                {
                    loc2 = false;
                }
            }
            var loc3=loc2 ? "#FFFF00" : "#FF0000";
            this._costLabel.htmlText = HTMLUtil.addColor("" + loc1, loc3);
            if (this.unit != EPrictUnit._EPriceUnitCoin) 
            {
                this._unitBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.Yuanbao);
            }
            else 
            {
                this._unitBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.Tongqian);
            }
            return;
        }

        public clearJewelTransItems(): void
        {
            this._currentOper.clearJewels();
            this.updateJewelDatas();
            return;
        }

        /* internal  */get unit(): int
        {
            if (typeof this._currentOper === "jewelexchangenormal") 
            {
                return EPrictUnit._EPriceUnitCoin;
            }
            return EPrictUnit._EPriceUnitGold;
        }

        /* internal */ /* var */_operBg: ScaleBitmap;

        /* internal */ /* var */_tabbarData: Array<any>;

        /* internal */ /* var */_typeTabBar: GTabBar;

        /* internal */ /* var */_currentOper: IJewelExchangeOper;

        /* internal */ /* var */_normalJewelOper: JewelExchangeNormal;

        /* internal */ /* var */_specialJewelOper: JewelExchangeSpecial;

        /* internal */ /* var */_desc: string;

        /* internal */ /* var */_descText: Object /* flash.text.TextField */;

        /* internal */ /* var */_startBtn: GButton;

        /* internal */ /* var */_costLabel: GLabel;

        /* internal */ /* var */_unitBm: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_jewelPanel: TilePanel;
    }
